from app.extensions import SessionLocal
from app.models.Clas import Clas
from app.repositories.ClasRepo import ClasRepository
from app.asserts import code
from fastapi import APIRouter, Response, status

class ClasService():
    def __init__(self, db: SessionLocal):
        # 创建DAO层对象
        self.repo = ClasRepository(db)

    def get_by_keyword(self, keyword):
        """
        根据关键字查询数据
        :param keyword: 关键字
        :return:
        """
        data = self.repo.get_by_keyword(keyword)
        return {
            "code": status.HTTP_200_OK,
            "data": data,
            "message": "查询成功"
        }
    def update_clas(self, clas_id, new_name):
        # clas_id 存在吗？
        clas = self.repo.get_clas_by_id(clas_id)
        if clas is None:
            return {
                "code": code.NOT_FOUND,
                "data": None,
                "message": "班级不存在"
            }
        new_clas = self.repo.get_clas_by_name(new_name)
        if new_clas is not None:
            return {
                "code": code.ALREADY_EXITED,
                "data": None,
                "message": "新的班级名称已经存在，无法修改"
            }
        # new_name 存在吗？
        result = self.repo.get_clas_by_name(new_name)
        if new_clas is not None:
            return {
                "code": code.ALREADY_EXITED,
                "data": None,
                "message": "新的班级名称已经存在，无法修改"
            }
        result = self.repo.update_clas(clas_id, new_name)
        if result:
            return {
                "code": code.SUCESS,
                "data": None,
                "message": "修改成功"
            }
        else:
            return {
                "code": code.SERVICE_ERROR,
                "data": None,
                "message": "修改失败"
            }


    def add_clas(self, clas_name):
        # 判断班级是否已经存在
        clas =  self.repo.get_clas_by_name(clas_name)
        if clas is not None:
#             班级已经存在了
            return {
                "code": code.ALREADY_EXITED,
                "data": None,
                "message": "班级已经存在"
            }
#         添加班级
        result = self.repo.add_clas(clas_name)
        if result:
            return {
                "code": code.SUCESS,
                "data": None,
                "message": "班级已创建成功"
            }
        return {
            "code": code.SERVICE_ERROR,
            "data": None,
            "message": "班级创建失败"
        }

    def delete_clas(self, clas_id):
        # clas_id 存在吗
        clas = self.repo.get_clas_by_id(clas_id)
        if clas is None:
            return {
                "code": status.HTTP_404_NOT_FOUND,
                "data": None,
                "message": "班级不存在"
            }
        # 删除
        result = self.repo.delete_clas(int(clas_id))
        if result:
            return {
                "code": status.HTTP_200_OK,
                "data": None,
                "message": "删除成功"
            }
        else:
            return {
                "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
                "data": None,
                "message": "删除失败"
            }
